
#ifdef __PSP__

#include "PSP.h"
#include "defs.h"

#include <string.h>
#include <malloc.h>

#define BUF_WIDTH 512
#define SCR_WIDTH 480
#define SCR_HEIGHT 272
#define PIXEL_SIZE 4
#define FRAME_SIZE BUF_WIDTH * SCR_HEIGHT * PIXEL_SIZE
#define ZBUF_SIZE BUF_WIDTH SCR_HEIGHT * 2
#define TEXT_LENGTH 128


static unsigned int __attribute__((aligned(16))) list[262144];
FILE * fichero;


void sdlSetup(){

	defines def;
	if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
	{
		printf("Unable to init SDL: %s\n", SDL_GetError());
	exit(1);
	}
	atexit(SDL_Quit);
	SDL_Surface * screen = SDL_SetVideoMode(WINDOW_WIDTH,WINDOW_HEIGHT, BITS_PER_PIXEL ,SDL_SWSURFACE);

	if ( screen == NULL )
	{
		printf("Unable to set %dx%d video: %s\n", def.GetWindowWidth(),def.GetWindowHeight(),SDL_GetError());
		exit(1);
	}
}


void sdlShutdown(){

}


void initGU() {
   
	sceGuInit();

	sceGuStart(GU_DIRECT, list);
	sceGuDrawBuffer(GU_PSM_8888, (void*) 0, BUF_WIDTH);
	sceGuDispBuffer(SCR_WIDTH, SCR_HEIGHT, (void*) 0x88000, BUF_WIDTH);
	sceGuDepthBuffer((void*) 0x110000, BUF_WIDTH);
	sceGuOffset(2048 - (SCR_WIDTH / 2), 2048 - (SCR_HEIGHT / 2));
	sceGuViewport(2048, 2048, SCR_WIDTH, SCR_HEIGHT);
	sceGuDepthRange(0xc350, 0x2710);
	sceGuScissor(0, 0, SCR_WIDTH, SCR_HEIGHT);
	sceGuEnable(GU_SCISSOR_TEST);
	sceGuDepthFunc(GU_GEQUAL);
	sceGuEnable(GU_DEPTH_TEST);
	sceGuFrontFace(GU_CW);
	sceGuShadeModel(GU_SMOOTH);
	sceGuEnable(GU_CULL_FACE);
	sceGuEnable(GU_CLIP_PLANES);
	sceGuEnable(GU_BLEND);
	sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
	sceGuFinish();
	sceGuSync(0, 0);

	sceDisplayWaitVblankStart();
	sceGuDisplay(GU_TRUE);
}


void initNet() {
	//Since we are User mode we have to use those funtions instead of pspSdkLoadInetModules().
	sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON);
	sceUtilityLoadNetModule(PSP_NET_MODULE_INET);

	//Those functions initalise the network libraries that are loaded with the previous functions.
	sceNetInit(128 * 1024, 42, 4 * 1024, 42, 4 * 1024);
	sceNetInetInit();
	sceNetApctlInit(0x8000, 48);
}


void deinitNet() {
	//You have to terminate the network libraries.
	//It has to be done in the opposite order you initalises them.
	sceNetApctlTerm();
	sceNetInetTerm();
	sceNetTerm();
}


void showConnectToAccesPointDialog() {

	bool isVideoInfoInitialized = SDL_WasInit(SDL_INIT_VIDEO) == SDL_INIT_VIDEO;;
        
	if (isVideoInfoInitialized) {
		sdlShutdown(); 
	}

	unsigned int* list = (unsigned int*)memalign(16, 1024 * 32);

	initGU();

	int done=0;

	pspUtilityNetconfData data;

	memset(&data, 0, sizeof (data));
	data.base.size = sizeof (data);
	sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_LANGUAGE, &data.base.language);
	sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_UNKNOWN, &data.base.buttonSwap);
	data.base.graphicsThread = 17;
	data.base.accessThread = 19;
	data.base.fontThread = 18;
	data.base.soundThread = 16;
	data.action = PSP_NETCONF_ACTION_CONNECTAP;

	struct pspUtilityNetconfAdhoc adhocparam;
	memset(&adhocparam, 0, sizeof (adhocparam));
	data.adhocparam = &adhocparam;

	sceUtilityNetconfInitStart(&data);

	while (!done) {
       
		//start GU for drawing
		sceGuStart(GU_DIRECT, list);
		sceGuClearColor(0x00000000);
		sceGuClearDepth(0);
		sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT);
		sceGuFinish();
		sceGuSync(0, 0);
 
		switch (sceUtilityNetconfGetStatus()) {
			case PSP_UTILITY_DIALOG_NONE:
				//done=1;
				break;
			case PSP_UTILITY_DIALOG_VISIBLE:
				sceUtilityNetconfUpdate(1);
				break;
			case PSP_UTILITY_DIALOG_QUIT:
				sceUtilityNetconfShutdownStart();
				done=0;
				break;
			case PSP_UTILITY_DIALOG_FINISHED:
				done=1;
				break;
			default:
				break;
		}

		// Swap buffers (waiting for vsync)
		sceDisplayWaitVblankStart();
		sceGuSwapBuffers();
	}
	sceGuTerm();
	free(list);
	if (isVideoInfoInitialized) {
		sdlSetup();
	}
}


char * showOSKDialog(char * descripcion, char * mensaje) {

	// Terminate SDL video info
	bool isVideoInfoInitialized = SDL_WasInit(SDL_INIT_VIDEO) == SDL_INIT_VIDEO;;
        
	if (isVideoInfoInitialized) {
    
		// Shutdown SDL
		sdlShutdown(); 
	}

	// Alloc 32Kb display list
	unsigned int* list = (unsigned int*)memalign(16, 1024 * 32);

	initGU();

	unsigned short intext[TEXT_LENGTH];
	unsigned short outtext[TEXT_LENGTH];
	unsigned short desc[TEXT_LENGTH];

	memset(&intext, 0, TEXT_LENGTH * sizeof(unsigned short));
	memset(&outtext, 0, TEXT_LENGTH * sizeof(unsigned short));
	memset(&desc, 0, TEXT_LENGTH * sizeof(unsigned short));

	int done=0;

	for(int y=0; y<strlen(mensaje); y++){
		intext[y] = mensaje[y];
	}

	for(int y=0; y<strlen(descripcion); y++){
		desc[y] = descripcion[y];
	}

	SceUtilityOskData data;

	memset(&data, 0, sizeof(SceUtilityOskData));
	data.language = PSP_UTILITY_OSK_LANGUAGE_DEFAULT; // Use system default for text input
	data.lines = 1;
	data.unk_24 = 1;
	data.inputtype = PSP_UTILITY_OSK_INPUTTYPE_ALL; // Allow all input types
	data.desc = desc;
	data.intext = intext;
	data.outtextlength = TEXT_LENGTH;
	data.outtextlimit = 32; // Limit input to 32 characters
	data.outtext = outtext;

	SceUtilityOskParams params;
	memset(&params, 0, sizeof(params));
	params.base.size = sizeof(params);
	sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_LANGUAGE, &params.base.language);
	sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_UNKNOWN, &params.base.buttonSwap);
	params.base.graphicsThread = 17;
	params.base.accessThread = 19;
	params.base.fontThread = 18;
	params.base.soundThread = 16;
	params.datacount = 1;
	params.data = &data;

	sceUtilityOskInitStart(&params);

	while(!done)
	{
		sceGuStart(GU_DIRECT,list);
		sceGuClearColor(0);
		sceGuClearDepth(0);
		sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT);

		sceGuFinish();
		sceGuSync(0,0);

		switch(sceUtilityOskGetStatus())
		{
			case PSP_UTILITY_DIALOG_INIT:
				break;
			
			case PSP_UTILITY_DIALOG_VISIBLE:
				sceUtilityOskUpdate(1);
				break;
			
			case PSP_UTILITY_DIALOG_QUIT:
				sceUtilityOskShutdownStart();
				break;
			
			case PSP_UTILITY_DIALOG_FINISHED:
				break;
				
			case PSP_UTILITY_DIALOG_NONE:
				done = 1;
				
			default :
				break;
		}

		sceDisplayWaitVblankStart();
		sceGuSwapBuffers();
	}
                
	// Shutdown GU
	sceGuTerm();

	// Free display list
	free(list);

	// Re-setup SDL video info
	if (isVideoInfoInitialized) {
    
		// Restart SDL
		sdlSetup(); // Replace with your own SDL startup code
	}

	char resultado [TEXT_LENGTH];
	int j;

	for(j = 0; data.outtext[j]; j++)
	{
		unsigned c = data.outtext[j];
		if(32 <= c && c <= 126){
			resultado[j] = (char) c;
		}
	}
	resultado[j++] = '\0';

	fichero = fopen("test","a");
	fprintf(fichero,"RES: %s\n",resultado);
	fclose(fichero);

	return resultado;
}


#endif


